<?php

class Source extends Controller
{
	function __construct()
	{			
		parent::__construct();
		
		$c = $this->uri->total_segments();
		if( $this->uri->segment($c)=="debug" )
		{
			$debug = true;
			error_reporting(E_ALL);
			$this->output->enable_profiler(TRUE);
		}else
		{
			$debug = false;
		}
				
	}
	function index()
	{
		$this->slist(0);
	}
	/*
	 * 
Array ( [count] => -1 [hasPrev] => 0 [hasNext] => 1 [contentList] => Array ( [0] => stdClass Object ( [id] => 150528 [realid] => 150528 [title] => J2EE 企业级培训教程(含源码) [tag] => J2EE 企业级培训教程 J2EE 企业级培训教程(含源码) J2EE教程 jav [categoryid] => 0 [source_money] => 0 [username] => chenxh [sourcesize] => 637952 [pubdate] => 1144378389 [updatetime] => 2011-06-07 14:04:36 [description] => J2EE 企业级培训教程(含源码) 注：　教程是英文版 [isrecommended] => 11 [status] => 1 [filetype] => rar [originfile] => j2eeqiyeqji.rar [fileaddr] => /2006/04/815/j2eeqiyeqji.rar [otherlink] => [clicknum] => 1276 [fileaddr_prefix] => /down1 [md5] => [isrepeat] => 0 )  
	 */
	function slist($catid,$pageno)
	{
		if(intval($catid)==0)
			$catid = intval($this->input->get("catid"));
		if(intval($pageno)==0)	
			$pageno = intval($this->input->get("pageno"));
		if($pageno==0) $pageno = 1;

		$this->load->library("sources_lib");
		$this->load->library("category_lib");
		
		if(intval($catid)>0)
		{
			$cname = strip_tags($this->category_lib->get_category_name($catid));
			$data['pagetitle'] = "$cname - 程序员资料下载";
		}else
		{
			$data['pagetitle'] = "首页 - 程序员资料下载";
		}
		$data['pagedescription'] = " Java Android C++ C# Delphi 程序员资料下载";	
		//echo $this->sources_lib->inc_click(1);
		$data['categories'] = $this->category_lib->get_categories_html();
		$slist = $this->sources_lib->get_list($catid,$pageno);
		
		$data['catid'] = $catid;
		$data['slist'] =  $slist['contentList'];
		if($slist['hasPrev'])
		{
			$data['prevurl'] = "/source/slist/$catid/".($pageno-1);
		}else
		{
			$data['prevurl'] = "";
		}
		if($slist['hasNext'])
		{
			$data['nexturl'] = "/source/slist/$catid/".($pageno+1);
		}else
		{
			$data['nexturl'] = "";
		}
		
		$this->load->view("source_list",$data);		
	}
	
	function ulist($username ,$pageno=1)
	{
		if($username=="")
			$username = trim($this->input->get("username"));
		if(intval($pageno)==0)
			$pageno = intval($this->input->get("pageno"));
		
		if($pageno==0) $pageno =1;		
		$data['pagetitle'] = "程序员资料下载";
		$data['pagedescription'] = "程序员资料下载";
		$this->load->library("sources_lib");
		$this->load->library("category_lib");
		//echo $this->sources_lib->inc_click(1);
		$data['categories'] = $this->category_lib->get_categories_html();
		$slist = $this->sources_lib->get_list_by_username($username,$pageno);
		
		$data['catid'] = 0;
		$data['slist'] =  $slist['contentList'];
		if($slist['hasPrev'])
		{
			$data['prevurl'] = "/source/ulist/$catid/".($pageno-1);
		}else
		{
			$data['prevurl'] = "";
		}
		if($slist['hasNext'])
		{
			$data['nexturl'] = "/source/ulist/$catid/".($pageno+1);
		}else
		{
			$data['nexturl'] = "";
		}
		
		$this->load->view("source_list",$data);		
	}	
	
	function detail($id,$username)
	{
		if(intval($id)==0)
			$id = $this->input->get('id');
		if($username == "")
			$username = $this->input->get('username');
		$this->load->library("sources_lib");
		$this->load->library("category_lib");
		$row = $this->sources_lib->get_info($id);
		$this->load->helper("common");
		if(empty($row['description_en']))
		{
			if($row['description'])
			{
				$ts = translate($row['description']);
				if($ts)
				{
					$object = new stdClass();
					$object->id = $id;
					$object->description_en=$ts;
					$this->load->model("sources_model");
					$this->sources_model->update($object);
					$row['description_en'] = $ts;
				}
			}
			
		}
		if($row['username']!=$username) exit;
		$data['catid'] = 0;
		
		$data['categories'] = $this->category_lib->get_categories_html();
		$data['row'] = $row;
		$this->sources_lib->inc_click($id);
		$prev = $this->sources_lib->get_prev_source($id);
		$next = $this->sources_lib->get_next_source($id);
		$data['prev'] = $prev;
		$data['next'] = $next;
		
		$data['pagetitle'] = $row['title']. " - ".strip_tags($row['categoryname'])." - ".  "程序员资料下载  ";
		$data['pagedescription'] = "程序员资料下载  ".mb_substr($row['description'],0,300);
		
		
		$this->load->view("source_detail",$data);
	}
	
	/*
	 * 
	 */
	function rss()
	{
		$catid = intval($this->input->get("catid"));
		
		header("Content-Type: application/xml; charset=utf-8"); 
		
		$this->load->library("sources_lib");
		$ret = ($this->sources_lib->get_list($catid));
		
		$items =  array();
		
		foreach($ret['contentList'] as $row)
		{
			$temp = array();
			$temp['title'] =  $row['title'];
			$temp['link'] =  $this->config->item("base_url"). "source/detail/{$row['id']}/{$row['username']}";
			$temp['description'] = $row['description'];
			$temp['pubdate'] = $row['pubdate'];
			$items[] = $temp;
		}
		$data['items'] =  $items;
		
		$this->load->view("rss_view",$data);		
	}
	
	function test()
	{
		$this->output->enable_profiler(TRUE);
		$this->load->library("sources_lib");
		//echo $this->sources_lib->inc_click(1);
		//print_r($this->sources_lib->get_list(16000));
		print_r( $this->sources_lib->get_info(1));
	}
	


		
	
}
